<template>
  <div>
    <!-- 左侧操作 -->
    <div class="operate" v-if="tyActive">
      <div class="cancle" @click="cancleClick">
        <img
          src="../../assets/img/cancle.png"
          alt=""
          style="width: 35px; height: 35px"
        />
        <div>取消</div>
      </div>
      <div class="carry_out" @click="carryOutClick">
        <img
          src="../../assets/img/carryOut.png"
          style="width: 35px; height: 35px"
          alt=""
        />
        <div>完成</div>
      </div>
    </div>
  </div>
</template>

<script>
import Turf from "turf";
import Graffiti from "../../model/Graffiti";
export default {
  props: {
    tyActive: {
      type: Boolean,
      default() {
        return false
      }
    },
    stateManager: {
      type: Object,
      default:null
    },
    map: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      coordinates: [],
      roundedArea: 0
    }
  },
  watch: {
    tyActive(newVal) {
      if (!newVal) {
        this.stateManager.removeCurrent();
      } else {
        this.coordinates = [];
        this.stateManager.setCurFun(new Graffiti(this.map,{callBack:this.update}));
      }
    }
  },
  methods: {
    update(coordinates){
      this.coordinates=coordinates;
    },
    //取消涂鸦点击
    cancleClick() {
      this.$emit("cancleClick")
      this.stateManager.removeDrawLayer();
      this.coordinates = [];
      this.stateManager.removeCurrent();
    },
    //完成涂鸦点击
    carryOutClick() {
      if(this.coordinates.length>=3){
        this.$emit("submitGraffiti",{
          type: "Polygon",
          coordinates: [this.coordinates],
        });
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.operate {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  padding: 5px;
  color: #7a7a7a;
  background-color: #f1edea;
  font-size: 12px;
  z-index: 1000;
}
.cancle,
.carry_out {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  background-color: #fff;
  cursor: pointer;
}
</style>
